package tgc.edu.mcy.web.controller;

import java.util.HashMap;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.mcy.custom.JpgridUtils;
import tgc.edu.mcy.custom.JqgridParam;
import tgc.edu.mcy.entity.TbClass;
import tgc.edu.mcy.service.TbClassService;
import tgc.edu.mcy.web.form.ClassForm;

@Controller
public class ClassController {
	@Autowired
	private TbClassService service;
	
	@RequestMapping(value="banji")
	public String banji() {
		return "banji";
	}
	
	@RequestMapping(value="/list")
	@ResponseBody
	//分页对象
	public Object List(JqgridParam param) {
		Pageable pageable = param.buildPageable();
		Page<TbClass> page = null;
		if(!param.getSearch()) {
			page = service.findAll(pageable);
		}else {
			/*if(param.getFilters() == null  || "".equals(param.getFilters())) {
				page = service.findAll(pageable);
			}else{*/
				page = service.findAll(param.buildSpec(), pageable);
			/*}*/
		}
		HashMap<String , Object> result = JpgridUtils.getPageResult(page);
		return result;
	}
	
	@RequestMapping(value="/edit")
	@ResponseBody
	public Object edit(String id, ClassForm form) {
		String oper = form.getOper();
		if("del".equals(oper)) {
			service.deleteById(Integer.parseInt(id));			
		}else if("add".equals(oper)) {
			TbClass model = new TbClass();
			BeanUtils.copyProperties(form, model);		//把form表单里边的值考到model中
			service.save(model);
		}else {
			TbClass model = new TbClass();
			BeanUtils.copyProperties(form, model);
			model.setId(Integer.parseInt(id));
			service.save(model);
		}
		return "ok";
	}
}
